package com.ht.demo.service.mapper.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.demo.dao.dao.system.SysUserPostDao;
import com.ht.demo.dao.domain.system.SysUserPostDO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author hdengxian
 * @Date 2023/5/26 15:32
 */
@Component
public class SysUserPostMapper extends ServiceImpl<SysUserPostDao, SysUserPostDO> {
    @Resource
    private SysUserPostDao sysUserPostDao;

    public Boolean savaUserPostRelation(Long sysUserId, List<Long> sysPostIds) {
        LambdaQueryWrapper<SysUserPostDO> delete = new LambdaQueryWrapper<>();
        delete.eq(SysUserPostDO::getSysUserId, sysUserId);
        sysUserPostDao.delete(delete);

        for (Long sysPostId : sysPostIds) {
            SysUserPostDO save = new SysUserPostDO();
            save.setSysPostId(sysPostId);
            save.setSysUserId(sysUserId);
            sysUserPostDao.insert(save);
        }
        return Boolean.TRUE;
    }

    public Integer countByPostId(Long sysPostId) {
        LambdaQueryWrapper<SysUserPostDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserPostDO::getSysPostId, sysPostId);
        Integer count = sysUserPostDao.selectCount(wrapper);
        if (count == null) count = 0;
        return count;
    }

    public List<Long> getPostIds(Long sysUserId) {
        LambdaQueryWrapper<SysUserPostDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(SysUserPostDO::getSysPostId);
        wrapper.eq(SysUserPostDO::getSysUserId, sysUserId);
        return sysUserPostDao.selectObjs(wrapper).stream().map(o -> Long.parseLong(o.toString())).collect(Collectors.toList());
    }
}
